<template>
    <div class="post-detail-page">
        <Modal title="删除文章" :visible="modalIsVisible" @modal-on-close="modalIsVisible = false"
            @modal-on-confirm="hideAndDelete">
            <p>确定要删除这篇文章吗？</p>
        </Modal>
        <article class="w-75 mx-auto mb-5 pb-3" v-if="currentPost">
            <img :src="currentImageUrl" alt="currentPost.title" class="rounded-lg img-fluid my-4"
                v-if="currentImageUrl">
            <h2 class="mb-4">{{ currentPost.title }}</h2>
            <div class="user-profile-component border-top border-bottom py-3 mb-5 align-items-center row g-0">
                <div class="col">
                    <UserProfile :user="currentPost.author" v-if="(typeof currentPost.author === 'object')">
                    </UserProfile>
                </div>
                <span class="text-muted col text-right font-italic">发表于：{{ currentPost.createdAt }}</span>
            </div>
            <div v-html="currentHTML"></div>
            <div v-if="showEditArea">
                <router-link class="btn btn-success" :to="{ name: 'create', query: { id: currentPost._id } }"
                    type="button">编辑</router-link>
                <button class="btn btn-danger" @click.prevent="modalIsVisible = true">删除</button>
            </div>
        </article>
    </div>
</template>
<script lang="ts">
import { defineComponent, computed, onMounted, ref } from 'vue'
import { useStore } from 'vuex'
import { useRoute, useRouter } from 'vue-router'
import { GlobalDataProps, PostProps, ImageProps, userProps, ResponseType } from '../store'
import MarkdownIt from 'markdown-it'
import UserProfile from '@/components/UserProfile.vue'
import Modal from '@/components/Modal.vue'
import CreateMessage from '@/components/CreateMessage'
export default defineComponent({
    name: 'PostDetail',
    components: {
        UserProfile,
        Modal
    },
    setup() {
        const modalIsVisible = ref(false)
        const store = useStore<GlobalDataProps>()
        const route = useRoute()
        const router = useRouter()
        const currentId = route.params.id
        const md = new MarkdownIt()
        const currentPost = computed<PostProps>(() => store.getters.getCurrentPost(currentId))
        const currentHTML = computed(() => {
            const { content, isHTML } = currentPost.value
            if (currentPost.value && content) {
                return isHTML ? content : md.render(content)
            }
        })
        const showEditArea = computed(() => {
            const { isLogin, _id } = store.state.user
            if (isLogin && currentPost.value && currentPost.value.author) {
                const postAuthor = currentPost.value.author as userProps
                return postAuthor._id === _id
            } else {
                return false
            }
        })
        const currentImageUrl = computed(() => {
            if (currentPost.value && currentPost.value.image) {
                const { image } = currentPost.value
                return (image as ImageProps).url + '?x-oss-process=image/resize,w_850'
            } else {
                return null
            }
        })
        const hideAndDelete = () => {
            modalIsVisible.value = false
            store.dispatch('deletePost', currentId).then((rawData: ResponseType<PostProps>) => {
                CreateMessage('删除成功，正在跳往首页...', 'success', 2000)
                setTimeout(() => {
                    router.push({ name: 'column', params: { id: rawData.data.column } })
                }, 2000)
            })
        }
        onMounted(() => {
            store.dispatch('fetchPost', currentId)
        })
        return {
            currentPost,
            currentImageUrl,
            currentHTML,
            showEditArea,
            modalIsVisible,
            hideAndDelete
        }
    }
})
</script>
